/*
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package catalog

import (
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
)

// Builder builds a Catalog to allow mappings between Go types and the corresponding
// GroupVersionHooks, metadata and OpenAPIDefinitions.
type Builder struct {
	// GroupVersion is the GroupVersion used when registering
	// Hooks and OpenAPIDefinitions.
	GroupVersion schema.GroupVersion

	// catalogBuilder are the functions which are used
	// to register Hooks and OpenAPIDefinitionsGetter into a catalog.
	catalogBuilder []func(*Catalog)

	// SchemeBuilder are the functions which are used
	// to add the types of Hook requests and responses to
	// the scheme of a catalog.
	schemeBuilder runtime.SchemeBuilder
}

// RegisterHook registers a Hook with its request and response types and metadata.
// The passed in hookFunc must have the following type: func(*RequestType,*ResponseType)
// The name of the func becomes the "hook" field of the GroupVersionHook.
func (bld *Builder) RegisterHook(hookFunc Hook, hookMeta *HookMeta) *Builder {
	bld.catalogBuilder = append(bld.catalogBuilder, func(catalog *Catalog) {
		catalog.AddHook(bld.GroupVersion, hookFunc, hookMeta)
	})
	return bld
}

// RegisterOpenAPIDefinitions registers a func returning the OpenAPIDefinitions for
// request and response types of all Runtime Hooks of a given group version.
// NOTE: The OpenAPIDefinitionsGetter funcs in the API packages are generated by
// openapi-gen.
func (bld *Builder) RegisterOpenAPIDefinitions(getter OpenAPIDefinitionsGetter) *Builder {
	bld.catalogBuilder = append(bld.catalogBuilder, func(c *Catalog) {
		c.AddOpenAPIDefinitions(getter)
	})
	return bld
}

// AddToCatalog adds all registered Hooks with their request and response types and metadata
// and the OpenAPIDefinitions to the catalog.
func (bld *Builder) AddToCatalog(catalog *Catalog) error {
	for _, addTo := range bld.catalogBuilder {
		addTo(catalog)
	}
	return bld.schemeBuilder.AddToScheme(catalog.scheme)
}

// Register adds a scheme setup function to the schemeBuilder.
// Note: This function is used by generated conversion code.
// If we would just expose the func from the embedded schemeBuilder
// directly it would not work because it is nil at that time and
// appending to a nil schemeBuilder doesn't propagate to our Builder.
func (bld *Builder) Register(f func(*runtime.Scheme) error) {
	bld.schemeBuilder.Register(f)
}

// Build returns a new Catalog containing all registered Hooks with their request and response types
// and metadata and the OpenAPIDefinitions.
func (bld *Builder) Build() (*Catalog, error) {
	s := New()
	return s, bld.AddToCatalog(s)
}
